package com.xinsoft.controller.oa;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.CusNewsDTO;
import com.xinsoft.entity.dto.CusNewsParam;
import com.xinsoft.service.CusNewsService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cus/news")
public class CusNewsController {

    @Autowired
    private CusNewsService cusNewsService;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Response<?> add(@RequestBody CusNewsDTO cusNews) {
        // 参数校验
        ValidatorUtils.validateEntity(cusNews);
        return Response.succeed(cusNewsService.addCusNews(cusNews));
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody CusNewsDTO cusNews) {
        // 参数校验
        ValidatorUtils.validateEntity(cusNews);
        return Response.succeed(cusNewsService.editCusNews(cusNews));
    }

    /**
     * 获取工位
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(cusNewsService.getCusNews(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        cusNewsService.del(ids);
        return Response.succeed();
    }

    /**
     * 发布
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/publish")
    public Response<?> audit(@RequestBody List<Integer> ids) {
        cusNewsService.publish(ids);
        return Response.succeed();
    }

    /**
     * 取消发布
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/dePublish")
    public Response<?> deAudit(@RequestBody List<Integer> ids) {
        cusNewsService.dePublish(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody CusNewsParam CusNewsPageParam) {
        return Response.succeed(cusNewsService.findPageListByParam(CusNewsPageParam));
    }
}
